package com.atguigu.userservice.controller.front;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.commonutils.ordervo.UserOrderVo;
import com.atguigu.commonutils.userVo.CommentUserVo;
import com.atguigu.userservice.entity.RepUser;
import com.atguigu.userservice.entity.vo.UserRequestVo;
import com.atguigu.userservice.service.RepUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ZhuangLiJiao
 * @since 2021-10-15
 */
@Data
class Communties {
    private String id;
    private String title;
}

@Api(description = "用户接口5001")
@RestController
@RequestMapping("/userservice/user")
@CrossOrigin
public class RepUserController {

    @Autowired
    private RepUserService userService;


    //12.获取全部社区
    @ApiOperation("获取全部社区")
    @GetMapping("getAllCommunities")
    public R getAllCommunities() {
        ArrayList<String> getAllCommunities = userService.getAllCommunities();
        ArrayList<Communties> allCommunities = new ArrayList<>();
        for (String abc : getAllCommunities) {
            Communties communties = new Communties();
            communties.setId(abc);
            communties.setTitle(abc);
            allCommunities.add(communties);
        }
        Communties communties = new Communties();
        communties.setId("全部");
        communties.setTitle("全部");
        allCommunities.add(communties);
        return R.ok().data("getAllCommunities", allCommunities);
    }


    //11 商家接单


    //10根据商家用户id获取商家信息
    @ApiOperation("根据商家用户id获取商家用户信息")
    @GetMapping("getMerchantById/{id}")
    public R getMerchantById(@PathVariable("id") String id) {
        RepUser user = userService.getById(id);
        return R.ok().data("getMerchantById", user);
    }


    //9.通过id获取用户信息
    @ApiOperation("通过id获取用户信息")
    @GetMapping("getCommentUserById/{userId}")
    public CommentUserVo getCommentUserById(@PathVariable("userId") String userId) {
        CommentUserVo commentUserVo = new CommentUserVo();
        RepUser byId = userService.getById(userId);
        BeanUtils.copyProperties(byId, commentUserVo);
        return commentUserVo;
    }

    //8.根据id获取用户信息
    @ApiOperation("根据id获取用户信息手机号码")
    @GetMapping("getRepUserMobileById/{id}")
    public String getRepUserMobileById(@PathVariable("id") String id) {
        RepUser member = userService.getById(id);
        return member.getMobile();
    }

    //8.根据id获取用户信息
    @ApiOperation("根据id获取用户信息")
    @GetMapping("getRepUserDetailById/{id}")
    public R getRepUserDetailById(@PathVariable("id") String id) {
        RepUser member = userService.getById(id);
        return R.ok().data("getRepUserDetailById", member);
    }

    //7.根据用户id获取用户信息
    @ApiOperation("根据用户信息获取用户id")
    @GetMapping("/getUserInfoById/{id}")
    public UserOrderVo getUserInfoById(@PathVariable("id") String id) {
        RepUser user = userService.getById(id);
        UserOrderVo vo = new UserOrderVo();
        BeanUtils.copyProperties(user, vo);
        return vo;
    }


    //6.登录，通过token获取用户信息
    @ApiOperation("通过token获取用户信息")
    @GetMapping("getMemberInfo")
    //从路径中获取 token信息
    // token是存放在请求头里面
    public R getMemberInfo(HttpServletRequest request) {
        //调用jwt工具类的方法。根据request对象获取头信息，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //查询数据库根据用户id获取用户信息
        RepUser repUser = userService.getById(memberId);
        return R.ok().data("userInfo", repUser);
    }

    //5.判断手机号是否已经注册
    @ApiOperation("判断手机号是否已经注册")
    @GetMapping("mobileExit/{mobile}")
    public R mobileExit(@PathVariable String mobile) {
        boolean exit = userService.mobileExit(mobile);
        return R.ok().data("mobileHasExit", exit);
    }

    //53.修改用户信息
    @ApiOperation("修改用户信息")
    @PostMapping("modify")
    public R modify(@RequestBody UserRequestVo.ModifyRepUserVo modifyRepUserVo) {
        boolean b = userService.modify(modifyRepUserVo);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }


    //4.修改其他信息
    @ApiOperation("修改其他信息")
    @PostMapping("modifyOtherMessage")
    public R modifyOtherMessage(@RequestBody UserRequestVo.ModifyOtherMessageVo modifyPasswordVo) {

        return R.ok();
    }


    //3.修改密码
    @ApiOperation("修改密码")
    @PostMapping("modifyPassword")
    public R modifyPassword(@RequestBody UserRequestVo.ModifyPasswordVo modifyPasswordVo) {
        //member对象封装手机号和密码
        //调用service方法实现登录
        //返回token值，使用jwt生成
        boolean b = userService.modifyPassword(modifyPasswordVo);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }


    //2.登录
    @ApiOperation("用户登录")
    @PostMapping("login")
    public R loginUser(@RequestBody UserRequestVo.LoginVo loginVo) {
        //member对象封装手机号和密码
        //调用service方法实现登录
        //返回token值，使用jwt生成
        String token = userService.login(loginVo);
        return R.ok().data("token", token);
    }

    //1.注册
    @ApiOperation("普通用户/商家用户注册")
    @PostMapping("register")
    public R registerUser(@RequestBody UserRequestVo.RegisterVo registerVo) {
        userService.register(registerVo);
        return R.ok();
    }
}

